"""
Author: xubing
Date: 2024-11-21
Description: 应用全局设置
"""

import os
from typing import Any, Dict, Optional

# 项目根目录
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# 日志配置
LOG_CONFIG = {
    "type": "db",  # 'db' 或 'file'
    "level": "INFO",
    "log_dir": os.path.join(BASE_DIR, "logs"),
    "max_bytes": 100 * 1024 * 1024,  # 100MB
    "backup_count": 5,
}

# 模型配置
MODEL_CONFIG = {
    "config_path": os.path.join(BASE_DIR, "model_config.yml"),
    "default_model": "qwen-max",
    "default_params": {
        "temperature": 0,
        "stream": False,
        "max_tokens": 2000,
    }
}

# 全局设置
SETTINGS = {
    "log_config": LOG_CONFIG,
    "model_config": MODEL_CONFIG,
}


def get_setting(key: str, default: Any = None) -> Any:
    """获取设置项"""
    keys = key.split('.')
    value = SETTINGS

    try:
        for k in keys:
            value = value[k]
        return value
    except (KeyError, TypeError):
        return default


def update_setting(key: str, value: Any) -> bool:
    """更新设置项"""
    keys = key.split('.')
    target = SETTINGS

    for k in keys[:-1]:
        if k not in target:
            target[k] = {}
        target = target[k]

    target[keys[-1]] = value
    return True


def get_model_param(model_name: Optional[str] = None,
                    param_name: Optional[str] = None) -> Any:
    """获取模型参数"""
    model = model_name or get_setting('model_config.default_model')
    params = get_setting('model_config.default_params', {})

    if param_name:
        return params.get(param_name)
    return params
